package com.itggm.controller;



import com.itggm.Result.ResponseResult;
import com.itggm.dto.DeptPageDTO;
import com.itggm.exception.BusinessException;
import com.itggm.pojo.Dept;
import com.itggm.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;

import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: ggm
 * @data 2024年12月21日 13:39
 */
@Slf4j
@RequestMapping("/dept")
@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

     /**
      * @Author: ggm
      * @date:  2024/12/21
      * 获取部门数据
      */
     @GetMapping("/list")
     public ResponseResult deptList(){
         log.info("开始获取部门数据");
         List<Dept> list = deptService.list();
         return  ResponseResult.success().data("deptList",list);
     }
      /**
       * @Author: ggm
       * @date:  2024/12/27
       * 部门列表分页查询
       */
      @PostMapping("/page")
      public ResponseResult deptPage(@RequestBody DeptPageDTO deptPageDTO){
          log.info("分页查询{}", deptPageDTO);
          return deptService.deptPage(deptPageDTO);

      }
       /**
        * @Author: ggm
        * @date:  2024/12/27
        * 添加或修改部门信息
        */
       @PostMapping("/saveOrUpdate")
       public ResponseResult addOrEditDept(@RequestBody Dept dept){
           log.info("添加或修改部门信息{}", dept);
           try {
               deptService.saveOrUpdate(dept);
           }catch (DuplicateKeyException e){
               throw  new BusinessException("部门名称重复");
           }
           return ResponseResult.success();

       }
        /**
         * @Author: ggm
         * @date:  2024/12/27
         * 删除部门信息
         */
        @DeleteMapping("/delete/{id}")
        public ResponseResult deleteDept(@PathVariable Integer id){
            log.info("删除部门信息{}", id);
            deptService.delete(id);
            return ResponseResult.success();

        }




}
